package com.woniuxy.project115.kaer.tenant.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project115.kaer.tenant.entity.OrderEvaluate;
import com.woniuxy.project115.kaer.tenant.entity.OrderLog;
import com.woniuxy.project115.kaer.tenant.entity.ServiceOrder;
import com.woniuxy.project115.kaer.tenant.mapper.OrderEvaluateMapper;
import com.woniuxy.project115.kaer.tenant.mapper.OrderLogMapper;
import com.woniuxy.project115.kaer.tenant.mapper.ServiceOrderMapper;
import com.woniuxy.project115.kaer.tenant.util.Data;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author HuH
 * @version 1.0
 * @CreatTime 2024-12-12 14:13
 * @description 订单自动评价
 */
@Component
public class EvaluateOrderConsumer {
    @Resource
    private ServiceOrderMapper serviceOrderMapper;

    @Resource
    private OrderEvaluateMapper orderEvaluateMapper;

    @Resource
    private OrderLogMapper orderLogMapper;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue("evaluate_order_queue"),
                    exchange = @Exchange("delay_ex"),
                    key = "evaluate_order"
            )
    )
    @RabbitHandler
    public void evaluateOrder(String serviceOrderNum) {
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(serviceOrderNum);
        OrderEvaluate evaluate = orderEvaluateMapper.selectOne(
                new QueryWrapper<OrderEvaluate>().eq("order_num", serviceOrderNum)
        );
        if (evaluate != null) {
            return;
        }
        evaluate = new OrderEvaluate();
        evaluate.setOrderNum(serviceOrderNum)
                .setStoreId(serviceOrder.getStoreId())
                .setEvaluatorId(0)
                .setEvaluatorName("系统")
                .setEvaluateTime(LocalDateTime.now())
                .setScore(5.0)
                .setContent("系统自动评价")
                .setCommentId(0)
                .setState(0);
        orderEvaluateMapper.insert(evaluate);

        serviceOrder.setState(Data.SERVICE_ORDER_COMPLETED)
                .setScore(5.0);
        serviceOrderMapper.updateById(serviceOrder);

        orderLogMapper.insert(
                new OrderLog()
                        .setOperator("系统")
                        .setServiceOrderNum(serviceOrderNum)
                        .setOperateTime(LocalDateTime.now())
                        .setOperateType("系统自动评价")
        );
    }
}
